package com.ywqf_api.controller;

import com.ywqf_api.dto.exc.ReturnExcution;
import com.ywqf_api.dto.in.MaintenanceDto;
import com.ywqf_api.entity.Maintenance;
import com.ywqf_api.enums.ReturnEnum;
import com.ywqf_api.service.MaintenanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/3 0003.
 */
@Api(description="maintenance接口",produces="application/json")
@Controller
@RequestMapping("maintenance")
public class MaintenanceController {
    @Autowired
    private MaintenanceService maintenanceService;

    /**
     * 查询客户正在进行的维修单和客户信息
     * @return
     */
    @ApiOperation(value = "查询客户正在进行的维修单，客户信息接口", httpMethod = "POST", notes = "查询客户正在进行的维修单，客户信息接口")
    @RequestMapping(value = "findMaintenance",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution findMaintenance(Integer houseId,Integer customerId){
        Map map = maintenanceService.findMaintenance(houseId,customerId);
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,map);
    }

    /**
     * 图片处理
     * @return
     */
    @ApiOperation(value = "图片处理接口", httpMethod = "POST", notes = "图片处理接口")
    @RequestMapping(value = "file",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution file(MultipartFile file){
        String url = maintenanceService.file(file);
        if(url == null || url.equals(null)){
            return new ReturnExcution(ReturnEnum.SAVE_FILE_FAIL,null);
        }
        url = "developer/"+url;
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,url);
    }

    /**
     * 维修申报
     * @param maintenanceDto
     * @return
     */
    @ApiOperation(value = "维修申报接口", httpMethod = "POST", notes = "维修申报接口")
    @RequestMapping(value = "saveMaintenance",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution saveMaintenance(MaintenanceDto maintenanceDto){
        int save = maintenanceService.saveMaintenance(maintenanceDto);
        return new ReturnExcution(ReturnEnum.ADD_SUCCESS,save);
    }


    /**
     * 维修申报+传图片
     * @param maintenanceDto
     * @return
     */
    @ApiOperation(value = "维修申报+传图片", httpMethod = "POST", notes = "维修申报+传图片")
    @RequestMapping(value = "saveUrlAndMaintenance",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution saveUrlAndMaintenance(MaintenanceDto maintenanceDto){
        int save = maintenanceService.saveUrlAndMaintenance(maintenanceDto);
        return new ReturnExcution(ReturnEnum.ADD_SUCCESS,save);
    }

    /**
     * 查询保修单
     * @param houseId
     * @return
     */
    @ApiOperation(value = "查询保修单接口", httpMethod = "POST", notes = "查询保修单接口")
    @RequestMapping(value = "findMainDetail",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution findMainDetail(Integer houseId){
        List<Maintenance> list = maintenanceService.findMainDetail(houseId);
        if(list == null){
            return new ReturnExcution(ReturnEnum.FIND_NULL_MAIN,null);
        }
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,list);
    }

    /**
     * 单条维修详情
     * @param maintenanceId
     * @return
     */
    @ApiOperation(value = "单条维修详情", httpMethod = "POST", notes = "单条维修详情")
    @RequestMapping(value = "findMainIdDetail",method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution findMainIdDetail(Integer maintenanceId){
        Map map = maintenanceService.findMainIdDetail(maintenanceId);
        if(map == null){
            return new ReturnExcution(ReturnEnum.FIND_NULL_MAIN,null);
        }
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,map);
    }


}
